import { cartGoodsList,deleteCartGoods,addCartGoodsNum } from '../../../../common/api/index'
Page({
    data: {
		cartList: [],
		pageNo: 1,
		total: 0,
		editType: 'submitOrder',
		totalMoney: 0,
	},
    onLoad(options) {

    },
    onShow() {
		this.getGoodsList()
	},
	// 加载购物车商品数据
	getGoodsList(){
		this.setData({
			showLoading: true
		})
		let data = {
			pageSize: 10,
			pageNo: this.data.pageNo,
		}
		cartGoodsList(data).then(res => {
			if(res.code == 200){
				res.data.forEach(item => {
					item.checked = false
				})
				let data = []
				if(this.data.pageNo == 1){
					data = res.data
				}else{
					data = this.data.cartList.concat(res.data)
				}
				this.setData({
					cartList: data,
					total: res.extend.total,
					showLoading: false
				})
			}
		})
	},
	// 勾选商品
	chooeseGoods(e){
		let index = e.currentTarget.dataset.index
		let dataList = this.data.cartList;
		let totalMoney = 0;
		dataList.forEach((item,ind) => {
			if(index == ind){
				item.checked = !item.checked
			}
			if(item.checked){
				totalMoney = Number(Number(totalMoney) + (Number(item.productPrice) * Number(item.buyCount)))
			}
		})
		this.setData({
			cartList: dataList,
			totalMoney: totalMoney,
		})
	},
	// 点击商品去详情
	clickToPath(e){
		let path = e.currentTarget.dataset.path
		wx.$goTo(path,true)
	},
	// 提交订单
	clickToSubmit(e){
		let arr = [];
		this.data.cartList.forEach(item => {
			if(item.checked){
				item['pic'] = item.productUrl
				item['price'] = item.productPrice
				arr.push(item)
			}
		})
		if(arr.length == 0){
			wx.showToast({
				title: '请先勾选下单商品',
				icon: 'none',
				duration: 2000
			})
			return
		}
		wx.setStorageSync('goodsInfo', arr)
		wx.removeStorageSync('chooeseAddrressData')//移除选择地址
		let path = e.currentTarget.dataset.path
		wx.$goTo(path,true)
	},
	// 触底加载
    onReachBottom() {
		if(this.data.total > this.data.cartList.length){
			this.setData({
				pageNo: ++this.data.pageNo,
			})
			this.getGoodsList()
		}
	},
	//点击编辑
	edit(){
		if(this.data.editType == 'submitOrder'){
			this.setData({
				editType: 'delete'
			})
		}else{
			this.setData({
				editType: 'submitOrder'
			})
		}
	},
	// 从购物车中删除商品
	deleteCartGoods(){
		let ids = []
		this.data.cartList.forEach(item => {
			if(item.checked){
				ids.push(item.cartId)
			}
		})
		if(ids.length == 0){
			wx.showToast({
				title: '请先勾选删除商品',
				icon: 'none',
				duration: 2000
			})
			return
		}
		let data = {
			cartId: ids.join(",")
		}
		deleteCartGoods(data).then(res => {
			if(res.code == 200){
				wx.showToast({
					title: '删除成功',
					icon: 'none',
					duration: 2000
				})
				this.setData({
					pageNo: 1
				})
				this.getGoodsList()
			}
		})
	},
	onChangeNum(e){
		let goodsData = e.currentTarget.dataset.item;
		// e.detail 数量
		// goodsData.cartId  购物车ID
		let str = "?num="+e.detail+'&cartId='+goodsData.cartId;
		addCartGoodsNum(str).then(res => {
			if(res.code == 200){
				let dataList = this.data.cartList;
				dataList.forEach(item => {
					if(item.cartId == goodsData.cartId){
						item.buyCount = e.detail
					}
				})
				this.setData({
					cartList: dataList
				})
			}
		})
	},



    /**
     * 生命周期函数--监听页面加载
     */

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})